<template>
  <div class="permission-container">
    <div class="app-container">
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click.native="addper(1,0)">添加权限</el-button>
        </div>
        <el-table
          v-loading="loading"
          :data="PermissionList"
          row-key="id"
          border
          style="width: 100%"
        >
          <el-table-column prop="name" label="名称" />
          <el-table-column prop="code" label="标识" />
          <el-table-column prop="description" label="描述" />
          <el-table-column label="操作">
            <template #default="{row}">
              <el-button v-if="row.type === 1" type="text" size="small" @click.native="addper(2,row.id)">添加权限点</el-button>
              <el-button type="text" size="small" @click="editper(row.id)">查看</el-button>
              <el-button type="text" size="small" @click="delper(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 新增编辑的弹层 -->
      <el-dialog
        :visible="showDialog"
        :title="showTitle"
        :close-on-click-modal="false"
        @close="closeDialog"
      >
        <!-- 表单内容 -->
        <el-form ref="refForm" label-width="100px" :model="formData" :rules="rules">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" />
          </el-form-item>
          <el-form-item label="权限启用">
            <el-switch
              v-model="formData.enVisible"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="clicksubmit">确定</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionListAPI, removePermissionAPI, addPermissionAPI, getPermissionAPI, editPermissionAPI } from '@/api/permission'
import { getlist } from '@/utils'
export default {
  name: 'Permission',
  data() {
    return {
      PermissionList: [],
      loading: false,
      showDialog: false,
      formData: {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [
          { required: true, message: '权限名称必填', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识称必填', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '权限描述必填', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    showTitle() {
      return this.formData.id ? '查看权限' : '添加权限'
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    // 获取权限列表方法
    async getPermissionList() {
      this.loading = true
      const { data } = await getPermissionListAPI()
      // console.log(data)
      this.PermissionList = getlist(data, '0') // 转换成树状数据
      this.loading = false
    },
    closeDialog() {
      this.showDialog = false
      this.formData = {
        enVisible: '0', // 启用禁用, 0禁用, 1启用
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型标记了一级(页面访问权) 二级(按钮操作权)
        pid: '' // 添加到哪个节点下
      }
    },
    // 添加的方法
    addper(type, pid) {
      this.showDialog = true
      this.formData.type = type
      this.formData.pid = pid
    },
    // 当点击确定后触发的方法
    clicksubmit() {
      this.$refs.refForm.validate(async valid => {
        if (!valid) return
        if (this.formData.id) {
        // console.log(this.formData)
          await editPermissionAPI(this.formData)
          this.$message.success('修改成功')
        } else {
          await addPermissionAPI(this.formData)
          this.$message.success('添加成功')
        }
        this.closeDialog()
        this.getPermissionList()
      })
    },
    // 删除权限的方法
    delper(id) {
      this.$confirm('你确定要删除吗?', '温馨提示').then(async() => {
        await removePermissionAPI(id)
        this.getPermissionList()
        this.$message.success('删除成功')
      }).catch(() => {})
    },
    // 点击查看编辑的方法
    async editper(id) {
      this.showDialog = true
      const { data } = await getPermissionAPI(id)
      // console.log(data)
      this.formData = data
    }
  }
}
</script>

<style>

</style>
